/* eslint-disable camelcase */
import { Tsrv, ifs, ts } from '@/services/';

const loadData = (filter: object, funName: string, tableName: string) => {
  const gpd = new ts.T_getPartDataArg();
  gpd.setFilter(filter);
  gpd.setFunName(tableName);
  gpd.setTableName(funName);
  gpd.setDbType('mysql');
  gpd.setLang('zh-CN');
  gpd.setPage(1, 10);
  gpd.setRule({} as ifs.I_rule);
  return Tsrv.getData(gpd);
};

export const getConfig = (
  scheduleNum: string
): Promise<{
  workshopsConfig: any;
  scheduleResultConfig: any;
}> => {
  console.log('getConfig called with scheduleNum:', scheduleNum);
  const filter = { f_code: { $eq: scheduleNum } };
  return new Promise((resolve, reject) => {
    loadData(filter, 'aps_schedule_info', 'aps_schedule_info')
      .then(([b, res, resp, m]) => {
        if (b && res.data.total > 0) {
          const scheduleRecordId = res.data.rows[0]['aps_schedule_record_id'];
          const f = { id: { $eq: scheduleRecordId } };
          loadData(f, 'aps_schedule_record', 'aps_schedule_record')
            .then(([b1, res1, resp1, m1]) => {
              if (b1 && res1.data.total > 0) {
                const scheduleResultConfig = res1.data.rows[0];
                const inUnzipResp = wUnZipStr(
                  scheduleResultConfig['input_data']
                );
                if (inUnzipResp.isErr()) {
                  throw new Error(inUnzipResp.getErrMsg());
                }
                // const inputData: string = inUnzipResp.getReturnData('data');
                const inputDataObj = inUnzipResp.getReturnData('data');

                const outUnzipResp = wUnZipStr(
                  scheduleResultConfig['output_data']
                );
                if (outUnzipResp.isErr()) {
                  throw new Error(outUnzipResp.getErrMsg());
                }
                // const outputData: string =outUnzipResp.getReturnData('data')
                const outputDataObj = outUnzipResp.getReturnData('data');
                // const inputDataObj = JSON.parse(inputData);
                // const outputDataObj = JSON.parse(outputData);
                resolve({
                  workshopsConfig: inputDataObj['workshops'],
                  scheduleResultConfig: {
                    info: {
                      endTime: outputDataObj['endTime'],
                      errorMessage: outputDataObj['errorMessage'],
                      processOrder: outputDataObj['processOrder'],
                      startTime: outputDataObj['startTime'],
                      success: outputDataObj['success'],
                      totalTime: outputDataObj['totalTime'],
                      workshopTimes: outputDataObj['workshopTimes']
                    },
                    workpieces: outputDataObj['workpieces']
                  }
                });
              } else {
                reject(new Error(m1));
              }
            })
            .catch(err => {
              reject(err);
            });
        } else {
          reject(new Error(m));
        }
      })
      .catch(err => {
        reject(err);
      });
  });
};
// // 默认导出组合配置
// export default {
//   workshops: workshopsConfig,
//   scheduleResult: scheduleResultConfig
// };
// 默认导出组合配置
export default {
  getConfig
};
